import { getAreaObj } from '@/utils/util'
import { cloneDeep } from 'lodash-es'

const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
		{
			label: '评估编号',
			prop: 'assessCode',
		},
		{
			label: '老人姓名',
			prop: 'oldmanName',
		},
		{
			label: '所属区域',
			prop: 'cr_areas_name',
		},
		{
			label: '身份证',
			prop: 'idCard',
		},
		// {
		// 	label: '年龄',
		// 	prop: 'age',
		// },
		{
			label: '初步等级',
			prop: 'firstAbilityLevelLabel',
		},
		{
			label: '最终等级',
			prop: 'finalAbilityLevelLabel',
		},
		{
			label: '老人照片',
			prop: 'oldmanPhotoOne',
			custome: 'img',
		},
		{
			label: '评估时间',
			prop: 'assessDate',
		},
		{
			label: '评估账号',
			prop: 'createUserName',
		},
		{
			label: '上链状态',
			prop: 'onBlockchainFlag',
			api: 'geiDictItemList',
			type: 'select',
			custome: 'tag',
			code: 'BLOCK_CHAIN_STATUS',
			defaultValue: '1',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const detailConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'left',
	column: [],
}
export const formConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'left',
	column: [
		{ label: '标题', prop: '', span: 8 },
		{ label: '动态大类', prop: '', span: 8 },
		{ label: '状态', prop: '', span: 8 },
		{ label: '是否必填', prop: '', span: 8 },
		{ label: '回答类型', prop: '', span: 8 },
		{ label: '排序', prop: '', span: 8 },
		{ label: '简介', prop: '', span: 8 },
		{ label: '答案选项', prop: '', span: 8 },
	],
}
const searchConfig = {
	inline: true,
	showBtn: true,
	labelPosition: 'left',
	column: [
		{
			label: '评估组织',
			prop: 'organizationName',
			type: 'input',
			span: 8,
		},
		{
			label: '老人所属区域',
			prop: 'areas_name',
			// propName:'areas_name',
			// regionGrade: 3,
			checkStrictly: false,
			type: 'AplidAreaSelect',
			span: 8,
		},
		{
			label: '评估时间段',
			prop: 'assessDateBetween',
			type: 'daterange',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 8,
		},
		{
			label: '评估编号',
			prop: 'assessCode',
			type: 'input',
			span: 8,
		},
		{
			label: '老人姓名',
			prop: 'oldmanName',
			type: 'input',
			span: 8,
		},
		{
			label: '身份证',
			prop: 'idCard',
			type: 'input',
			span: 8,
		},
		{
			label: '最终能力等级',
			prop: 'finalAbilityLevel',
			propName: 'finalAbilityLevelLabel',
			code: 'ACCESS_LEVEL_COUNTRY',
			api: 'getDictItemList',
			type: 'remoteSelect',
			span: 8,
		},
		{
			label: '经济来源',
			prop: 'economicSource',
			// propsName:'economicSourceLabels',
			code: 'INCOME_SOURCE',
			api: 'getDictItemList',
			multiple: true,
			type: 'remoteSelect',
			span: 8,
		},
		{
			label: '居住情况',
			prop: 'livingType',
			// propName:'livingTypeLabels',
			code: 'LIVE_SITUATE',
			api: 'getDictItemList',
			multiple: true,
			type: 'remoteSelect',
			span: 8,
		},
		{
			label: '评估原因',
			prop: 'assessReason',
			propName: 'assessReasonLabel',
			multiple: false,
			code: 'ASSESS_REASON',
			api: 'getDictItemList',
			type: 'remoteSelect',
			span: 8,
		},
		{
			label: '评估账号',
			prop: 'createUserName',
			type: 'input',
			span: 8,
		},
		{
			label: '年龄范围',
			prop: 'birthdayBetween',
			noRules: true,
			type: 'RangeValue',
			span: 8,
		},
	],
}

export const api = {
	index: 'view_bscElderlyAssessBaseInfo_page',
	// add: 'view_bscElderlyAssessBaseInfo_add',
	edit: 'view_bscElderlyAssessBaseInfo_edit',
	delete: 'view_bscElderlyAssessBaseInfo_del',
	export: 'view_bscElderlyAssessBaseInfo_exportTable',
	detail: 'view_bscElderlyAssessBaseInfo_view',
}
export const baseConfig: any = {
	pageName: '老年人能力评估记录',
	addLabel: '添加',
	dictcodes: [ 'ASSESS_REASON', 'LIVE_SITUATE', 'INCOME_SOURCE', 'ACCESS_LEVEL_COUNTRY', 'BLOCK_CHAIN_STATUS'],
	rowKey: 'id',
	tipLabel: 'assessCode',
	exportMqTopic: '/ning/export/',
	api: {
		...api,
	},
	auth: {
		...api,
		mulitSet: 'mulitSet',
		accessOldman: true, // 打开特殊导出
	},

	menuBtn: [
		{
			icon: 'detail',
			label: '详情',
			type: 'primary',
			operate: 'detail',
			auth: api.index,
			text: true,
			show: true,
		},
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: api.edit,
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: api.delete,
			text: true,
			show: true,
		},
	],
	formConfig,
	detailConfig,
	dialogConfig: {
		width: '65%',
	},
	searchConfig,
	tableConfig,
	beforeOnLoadFormate: async (query) => {
		query = cloneDeep(query)
		if (query.birthdayBetween) {
			let wuList = query.birthdayBetween.split(',')
			let currentYear = new Date().getFullYear()
			let lastYear = Number(currentYear) - Number(wuList[0]) + '-12-31'
			let firstYear = Number(currentYear) - Number(wuList[1]) + '-01-01'
			query.birthdayBetween = firstYear + ',' + lastYear
		}
		if (query.economicSource) {
			query.economicSourceList = query.economicSource.split(',')
		}
		if (query.livingType) {
			query.livingTypeList = query.livingType.split(',')
		}
		let CrAreaObj = getAreaObj(query.areas_name, 'cr')
		if (query.assessDateBetween) {
			query.assessDateBetween = query.assessDateBetween.join(',')
		}
		return new Promise((resolve, reject) => {
			resolve({
				...query,
				...CrAreaObj,
				areas_name: undefined,
				economicSource: undefined,
				livingType: undefined,
			})
		})
	},
	operateFormate: async (form) => {
		let item = cloneDeep(form)
		let cr_areas_name = getAreaObj(item, 'cr')
		return Promise.resolve({
			...item,
			...cr_areas_name,
		})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			let cr_areas_name = getAreaObj(item, 'cr')
			return {
				...item,
				cr_areas_name,
			}
		})
		return Promise.resolve(Object.freeze(tableData))
	},
}
